package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.RcglSignPointConvert;
import com.engine4cloud.tdf.supervision.dto.create.RcglSignPointCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.RcglSignPointQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglSignPointResultDTO;
import com.engine4cloud.tdf.supervision.entity.RcglSignPointEntity;
import com.engine4cloud.tdf.supervision.mapper.RcglSignPointMapper;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.RcglSignPointService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;


/**
 * 定点签到表
 *
 * @author wenjie
 * @date 2024-09-27 11:39:15
 */
@Service
public class RcglSignPointServiceImpl extends ServiceImpl<RcglSignPointMapper, RcglSignPointEntity> implements RcglSignPointService {

    @Resource
    private JzdxService jzdxService;

    @Override
    public PageInfo<RcglSignPointResultDTO> pageList(Params pageable, RcglSignPointQueryDTO dto) {
        Page<RcglSignPointResultDTO> page = this.baseMapper.pageList(Pages.page(pageable), dto);
        return Pages.convert(page);
    }

    @Override
    public RcglSignPointResultDTO getById(Long id) {
        return RcglSignPointConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(RcglSignPointCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        return super.save(RcglSignPointConvert.INSTANCE.create(dto, dx.getTenantId()));
    }
}